package pdfsetter.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;

import pdfsetter.core.pages.PageInfo;
import pdfsetter.core.pages.PageRectangle;
import pdfsetter.core.pages.PageType;
import pdfsetter.core.schema.DocumentSchema;
import pdfsetter.core.schema.FillingSchema;
import pdfsetter.core.schema.FixedFillingSchema;
import pdfsetter.core.schema.PageBuildSchema;
import pdfsetter.core.schema.PageInsertSchema;
import pdfsetter.core.schema.PageSelection;
import pdfsetter.core.schema.SimpleDocumentSchema;
import pdfsetter.core.schema.SinglePageSchema;
import pdfsetter.impl.pdfbox.compiler.PdfBoxDocumentCompiler;

public class SettingExample {
    public static void main(String args[]) {
        System.out.println("Hello! This is simple schemas setting example.");

        PageInfo a4page = new PageInfo(PageType.A4).rotate();
        ArrayList<PageRectangle> locations = new ArrayList<PageRectangle>();
        locations.add(new PageRectangle(0, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        locations.add(new PageRectangle(a4page.getWidth() / 2, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        SinglePageSchema pageSchema = new SinglePageSchema("2xA5 on A4", a4page, locations);

        List<PageSelection> pageSelectionsList = new LinkedList<PageSelection>();
        pageSelectionsList.add(new PageSelection(0, 5));
        pageSelectionsList.add(new PageSelection(0, 6));
        pageSelectionsList.add(new PageSelection(0, 1));
        FillingSchema fillSchema = new FixedFillingSchema(pageSelectionsList);

        DocumentSchema doc = new SimpleDocumentSchema(pageSchema, fillSchema);

        for (PageBuildSchema pbs : doc.getPageBuildSchemas()) {
            PageInfo pinfo = pbs.getPageInfo();
            System.out.println("Page info: " + pinfo.getHeight() + "(h), " + pinfo.getWidth() + "(w)");
            for (PageInsertSchema pis : pbs.getPageInsertSchemas()) {
                PageRectangle prec = pis.getDestinationPageRectangle();
                System.out.println("Rectangle: " + prec.getHeight() + "(h), " + prec.getWidth() + "(w), " + prec.getHorizontalShift() + "(h shift), "
                        + prec.getVerticalShift() + "(w shift), " + prec.getRotation() + "(r)");
                PageSelection psel = pis.getPageSelection();
                System.out.println("Selection: " + psel.getFileNumber() + "(f num), " + psel.getPageNumber() + "(p num)");
            }
            System.out.println("-----------");
        }

        // ------------ Input Files --------------
        List<File> files = new LinkedList<File>();
        files.add(new File("src/test/resources/test.pdf"));
        files.add(new File("src/test/resources/LISTING.pdf"));

        // Load source documents and retrieve document sizes
        for (File sourceFile : files) {
            try {
                PDDocument pdd = PDDocument.loadNonSeq(sourceFile, null);
                System.out.println("File info: " + pdd.getNumberOfPages() + "(pages)");
                pdd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // ------------ Compile -------------
        File out = new File("out-test/out_pdf.pdf");
        new PdfBoxDocumentCompiler().compile(doc, files, out);
    }
}
